<template>
  <div class="app-container">
    <div class="search-wrapper">
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="科室名称">
          <el-input v-model="queryParams.name" placeholder="请输入科室名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="医生姓名">
          <el-input v-model="queryParams.dockerName" placeholder="请输入医生姓名" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:department:add']"
          class="operation-btn"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:department:edit']"
          class="operation-btn"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:department:remove']"
          class="operation-btn"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:department:export']"
          class="operation-btn"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="departmentList"
      @selection-change="handleSelectionChange"
      border
      stripe
      highlight-current-row
      class="department-table"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="科室ID" align="center" prop="id" />
      <el-table-column label="医生" align="center" prop="dockerName">
        <template slot-scope="scape">
          <span v-if="scape.row.dockerName">{{ scape.row.dockerName }}</span>
          <span v-else>undefined</span>
        </template>
      </el-table-column>
      <el-table-column label="医院名称" align="center" prop="hospitalName" >
        <template slot-scope="scape">
          <span v-if="scape.row.hospitalName">{{ scape.row.hospitalName }}</span>
          <span v-else>undefined</span>
        </template>
      </el-table-column>
      <el-table-column label="医院地址" align="center" prop="hospitaladdress" >
        <template slot-scope="scape">
          <span v-if="scape.row.hospitaladdress">{{ scape.row.hospitaladdress }}</span>
          <span v-else>undefined</span>
        </template>
      </el-table-column>
      <el-table-column label="院区" align="center" prop="areaName" >
        <template slot-scope="scape">
          <span v-if="scape.row.areaName">{{ scape.row.areaName }}</span>
          <span v-else>undefined</span>
        </template>
      </el-table-column>
      <el-table-column label="院区地址" align="center" prop="areaAddress" >
        <template slot-scope="scape">
          <span v-if="scape.row.areaAddress">{{ scape.row.areaAddress }}</span>
          <span v-else>undefined</span>
        </template>
      </el-table-column>
      <el-table-column label="科室名称" align="center" prop="name" />
      <el-table-column label="状态：0停用，1启用" align="center" prop="status" >
        <template slot-scope="scope">
            <span v-if="scope.row.status === 1">
              <el-button type="success" icon="el-icon-check" circle></el-button>
            </span>
          <span v-else>
              <el-button type="danger" icon="el-icon-close" circle></el-button>
            </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:department:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:department:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      class="pagination-container"
    />

    <!-- 添加或修改科室对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
      class="department-dialog"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属医院" prop="hospitalId">
<!--          <el-input v-model="form.hospitalId" placeholder="请输入所属医院ID" />-->
          <el-select v-model="form.hospitalId" placeholder="医院院区">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.address"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入科室名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDepartment, getDepartment, delDepartment, addDepartment, updateDepartment } from "@/api/system/department"
import {getlist} from "@/api/system/area";

export default {
  name: "Department",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 科室表格数据
      departmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      options: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        dockerName:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hospitalId: [
          { required: true, message: "所属医院ID不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "科室名称不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询科室列表 */
    getList() {
      this.loading = true
      listDepartment(this.queryParams).then(response => {
        this.departmentList = response.rows
        this.total = response.total
        this.loading = false
      })
      getlist().then(res=>{
        this.options = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hospitalId: null,
        name: null,
        status: null,
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加科室"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDepartment(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改科室"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDepartment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addDepartment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除科室编号为"' + ids + '"的数据项？').then(function() {
        return delDepartment(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/department/export', {
        ...this.queryParams
      }, `department_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 84px);

  .search-wrapper {
    background-color: #fff;
    padding: 20px;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    margin-bottom: 20px;

    .search-form {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
    }
  }

  .operation-btn {
    transition: all 0.3s;
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    }
  }

  .department-table {
    margin-top: 20px;
    border-radius: 4px;
    overflow: hidden;

    ::v-deep .el-table__header-wrapper {
      th {
        background-color: #f5f7fa;
        color: #606266;
        font-weight: bold;
      }
    }

    ::v-deep .el-table__row {
      transition: all 0.3s;
      &:hover {
        background-color: #f0f9eb !important;
      }
    }
  }

  .pagination-container {
    margin-top: 20px;
    text-align: right;
  }

  .department-dialog {
    ::v-deep .el-dialog__body {
      padding: 20px 30px;
    }

    ::v-deep .el-form-item__label {
      font-weight: bold;
    }

    ::v-deep .el-input__inner {
      border-radius: 4px;
    }
  }
}

// 响应式布局
@media screen and (max-width: 768px) {
  .app-container {
    padding: 10px;

    .search-wrapper {
      padding: 10px;
    }

    .el-form-item {
      margin-bottom: 10px;
    }
  }
}
</style>
